"""
Copyright © 2023 Walkline Wang (https://walkline.wang)
Gitee: https://gitee.com/walkline/micropython-ws2812-led-clock
"""
import struct
from utime import sleep_ms
from micropython import const
from machine import SoftI2C, I2C, Pin


#region registers
REG_WHO_AM_I = const(0x0f)

REG_OUT_TEMP_L     = const(0x0c)
REG_OUT_TEMP_H     = const(0x0d)
REG_STATUS_REG_AUX = const(0x07)
REG_TEMP_CFG_REG   = const(0x1f)

REG_CTRL_REG0 = const(0x1e)
REG_CTRL_REG1 = const(0x20)
REG_CTRL_REG2 = const(0x21)
REG_CTRL_REG3 = const(0x22)
REG_CTRL_REG4 = const(0x23)
REG_CTRL_REG5 = const(0x24)
REG_CTRL_REG6 = const(0x25)

REG_REFERENCE = const(0x26)

REG_STATUS_REG = const(0x27)
REG_OUT_X_L    = const(0x28)
REG_OUT_X_H    = const(0x29)
REG_OUT_Y_L    = const(0x2a)
REG_OUT_Y_H    = const(0x2b)
REG_OUT_Z_L    = const(0x2c)
REG_OUT_Z_H    = const(0x2d)

REG_FIFO_CTRL_REG = const(0x2e)
REG_FIFO_SRC_REG  = const(0x2f)

REG_INT1_CFG = const(0x30)
REG_INT1_SRC = const(0x31)
REG_INT1_THS = const(0x32)
REG_INT1_DUR = const(0x33)

REG_INT2_CFG = const(0x34)
REG_INT2_SRC = const(0x35)
REG_INT2_THS = const(0x36)
REG_INT2_DUR = const(0x37)

REG_CLICK_CFG = const(0x38)
REG_CLICK_SRC = const(0x39)
REG_CLICK_THS = const(0x3a)

REG_TIME_LIMIT   = const(0x3b)
REG_TIME_LATENCY = const(0x3c)
REG_TIME_WINDOW  = const(0x3d)

REG_ACT_THS = const(0x3e)
REG_ACT_DUR = const(0x3f)


# 寄存器控制位

# == CTRL_REG0 ==
BIT_SDO_PU_DISC = 7

# == CTRL_REG1 ==
# BIT_ORD3 = 7
# BIT_ORD2 = 6
# BIT_ORD1 = 5
BIT_ORD0 = 4
BIT_LPEN = 3 # Low-power mode enable

# == CTRL_REG2 ==

# == CTRL_REG3 ==

# == CTRL_REG4 ==
BIT_BDU = 7 # Block Data Update
BIT_BLE = 6 # Big/Little Endian data selection
# BIT_FS1 = 5
BIT_FS0 = 4 # Full-scale configuration
BIT_HR  = 3 # Operating mode selection
BIT_SIM = 0 # SPI serial interface mode selection

# == CTRL_REG5 ==
BIT_BOOT = 7 # Reboot memory content

# == CLICK_CFG ==
BIT_ZD = 5 # Enable interrupt double-click on Z-axis
BIT_ZS = 4 # Enable interrupt single-click on Z-axis
BIT_YD = 3 # Enable interrupt double-click on Y-axis
BIT_YS = 2 # Enable interrupt single-click on Y-axis
BIT_XD = 1 # Enable interrupt double-click on X-axis
BIT_XS = 0 # Enable interrupt single-click on X-axis

# == STATUS_REG ==
BIT_ZYXOR = 7 # Info about device status
BIT_ZOR   = 6
BIT_YOR   = 5
BIT_XOR   = 4
BIT_ZYXDA = 3
BIT_ZDA   = 2
BIT_YDA   = 1
BIT_XDA   = 0

# == STATUS_REG_AUX ==
BIT_TOR = 6 # Temperature data available
BIT_TDA = 2 # Temperature data overrun
#endregion registers


PROPERTY_DISABLED = 0
PROPERTY_ENABLED  = 1


class LIS2DH12Exception(BaseException):
	pass


class LIS2DH12(object):
	#region constants
	ID = const(0x33) # (Who am I)

	LITTLE_ENDIAN = 0
	BIG_ENDIAN    = 1
	DATA_FORMATS   = {
		LITTLE_ENDIAN: 'little',
		BIG_ENDIAN   : 'big'
	}

	# operating mode selection
	MODE_NORMAL_10BIT   = 0b00
	MODE_HIGH_RES_12BIT = 0b01
	MODE_LOW_POWER_8BIT = 0b10
	OPERATING_MODES      = {
		MODE_NORMAL_10BIT   : 'normal (10bit)',
		MODE_HIGH_RES_12BIT : 'high-resolution (12bit)',
		MODE_LOW_POWER_8BIT : 'low-power (8bit)'
	}

	# output data rate selection
	ORD_POWER_DOWN          = 0B0000
	ORD_1HZ                 = 0B0001
	ORD_10HZ                = 0B0010
	ORD_25HZ                = 0B0011
	ORD_50HZ                = 0B0100
	ORD_100HZ               = 0B0101
	ORD_200HZ               = 0B0110
	ORD_400HZ               = 0B0111
	ORD_1620HZ_LP           = 0B1000
	ORD_5376HZ_LP_1344HZ_NM = 0B1001
	OUTPUT_DATA_RATES       = {
		ORD_POWER_DOWN          : 'power down',
		ORD_1HZ                 : 'HNL 1Hz',
		ORD_10HZ                : 'HNL 10Hz',
		ORD_25HZ                : 'HNL 25Hz',
		ORD_50HZ                : 'HNL 50Hz',
		ORD_100HZ               : 'HNL 100Hz',
		ORD_200HZ               : 'HNL 200Hz',
		ORD_400HZ               : 'HNL 400Hz',
		ORD_1620HZ_LP           : 'Low 1.620kHz',
		ORD_5376HZ_LP_1344HZ_NM : 'Low 5.376kHz, HN 1.344kHz'
	}

	FULL_SCALE_2G  = 0b00
	FULL_SCALE_4G  = 0b01
	FULL_SCALE_8G  = 0b10
	FULL_SCALE_16G = 0b11
	FULL_SCALES    = {
		FULL_SCALE_2G  : '2G',
		FULL_SCALE_4G  : '4G',
		FULL_SCALE_8G  : '8G',
		FULL_SCALE_16G : '16G'
	}

	SPI_MODE_4_WIRE = 0
	SPI_MODE_3_WIRE = 1
	SPI_MODES       = {
		SPI_MODE_4_WIRE: 'SPI 4 Wire',
		SPI_MODE_3_WIRE: 'SPI 3 Wire'
	}

	SDO_PULL_UP_CONNECT    = 0
	SDO_PULL_UP_DISCONNECT = 1
	SDO_PULL_UP_MODES      = {
		SDO_PULL_UP_DISCONNECT: 'SDO Pull-UP Disconnect',
		SDO_PULL_UP_CONNECT   : 'SDO Pull-UP Connect'
	}

	TEMP_ENABLED  = 3 << 6
	TEMP_DISABLED = 0

	IIC_ADDRS = (
		0x18, # 001 1000 | connected to gnd
		0x19  # 001 1001 | connected to vcc
	)
	#endregion constants


	def __init__(self, i2c:SoftI2C=None, i2c_addr=IIC_ADDRS[1]):
		assert isinstance(i2c, (SoftI2C, I2C)), LIS2DH12Exception('i2c is not I2C instance')
		assert i2c_addr in LIS2DH12.IIC_ADDRS, LIS2DH12Exception('invalid i2c address')

		self.__i2c = i2c
		self.__i2c_addr = i2c_addr

	def device_id(self):
		'''获取芯片 ID'''
		return self.__read_mem(REG_WHO_AM_I)[0]

	def reset(self):
		'''软复位寄存器数据'''
		# sleep_ms(5)

		# self.__write_mem(REG_CTRL_REG0,     0x10)
		# self.__write_mem(REG_CTRL_REG1,     0x07)
		# self.__write_mem(REG_CTRL_REG2,     0x00)
		# self.__write_mem(REG_CTRL_REG3,     0x00)
		# self.__write_mem(REG_CTRL_REG4,     0x00)
		# self.__write_mem(REG_CTRL_REG5,     0x00)
		# self.__write_mem(REG_CTRL_REG6,     0x00)
		# self.__write_mem(REG_TEMP_CFG_REG,  0x00)
		# self.__write_mem(REG_REFERENCE,     0x00)
		# self.__write_mem(REG_FIFO_CTRL_REG, 0x00)
		# self.__write_mem(REG_INT1_CFG,      0x00)
		# self.__write_mem(REG_INT1_THS,      0x00)
		# self.__write_mem(REG_INT1_DURATION, 0x00)
		# self.__write_mem(REG_INT2_CFG,      0x00)
		# self.__write_mem(REG_INT2_THS,      0x00)
		# self.__write_mem(REG_INT2_DURATION, 0x00)
		# self.__write_mem(REG_CLICK_CFG,     0x00)
		# self.__write_mem(REG_CLICK_THS,     0x00)
		# self.__write_mem(REG_TIME_LIMIT,    0x00)
		# self.__write_mem(REG_TIME_LATENCY,  0x00)
		# self.__write_mem(REG_TIME_WINDOW,   0x00)
		# self.__write_mem(REG_ACT_THS,       0x00)
		# self.__write_mem(REG_ACT_DUR,       0x00)

		data = self.__read_mem(REG_CTRL_REG5)[0]
		self.__write_mem(REG_CTRL_REG5, self.__set_bit(data, BIT_BOOT))
		sleep_ms(10)
		self.__write_mem(REG_CTRL_REG5, self.__cls_bit(data, BIT_BOOT))

	def is_zyx_data_ready(self):
		return self.__get_bit(self.__read_mem(REG_STATUS_REG)[0], BIT_ZYXDA)

	def get_acceleration(self):
		mode = self.operating_mode
		full_scale = self.full_scale
		x, y, z = struct.unpack('<hhh', self.__read_mem(REG_OUT_X_L, 6))

		if mode == LIS2DH12.MODE_LOW_POWER_8BIT:
			if full_scale == LIS2DH12.FULL_SCALE_2G:
				x = self.__from_fs2_lp_to_mg(x)
				y = self.__from_fs2_lp_to_mg(y)
				z = self.__from_fs2_lp_to_mg(z)
			elif full_scale == LIS2DH12.FULL_SCALE_4G:
				x = self.__from_fs4_lp_to_mg(x)
				y = self.__from_fs4_lp_to_mg(y)
				z = self.__from_fs4_lp_to_mg(z)
			elif full_scale == LIS2DH12.FULL_SCALE_8G:
				x = self.__from_fs8_lp_to_mg(x)
				y = self.__from_fs8_lp_to_mg(y)
				z = self.__from_fs8_lp_to_mg(z)
			elif full_scale == LIS2DH12.FULL_SCALE_16G:
				x = self.__from_fs16_lp_to_mg(x)
				y = self.__from_fs16_lp_to_mg(y)
				z = self.__from_fs16_lp_to_mg(z)
		elif mode == LIS2DH12.MODE_NORMAL_10BIT:
			if full_scale == LIS2DH12.FULL_SCALE_2G:
				x = self.__from_fs2_nm_to_mg(x)
				y = self.__from_fs2_nm_to_mg(y)
				z = self.__from_fs2_nm_to_mg(z)
			elif full_scale == LIS2DH12.FULL_SCALE_4G:
				x = self.__from_fs4_nm_to_mg(x)
				y = self.__from_fs4_nm_to_mg(y)
				z = self.__from_fs4_nm_to_mg(z)
			elif full_scale == LIS2DH12.FULL_SCALE_8G:
				x = self.__from_fs8_nm_to_mg(x)
				y = self.__from_fs8_nm_to_mg(y)
				z = self.__from_fs8_nm_to_mg(z)
			elif full_scale == LIS2DH12.FULL_SCALE_16G:
				x = self.__from_fs16_nm_to_mg(x)
				y = self.__from_fs16_nm_to_mg(y)
				z = self.__from_fs16_nm_to_mg(z)
		elif mode == LIS2DH12.MODE_HIGH_RES_12BIT:
			if full_scale == LIS2DH12.FULL_SCALE_2G:
				x = self.__from_fs2_hr_to_mg(x)
				y = self.__from_fs2_hr_to_mg(y)
				z = self.__from_fs2_hr_to_mg(z)
			elif full_scale == LIS2DH12.FULL_SCALE_4G:
				x = self.__from_fs4_hr_to_mg(x)
				y = self.__from_fs4_hr_to_mg(y)
				z = self.__from_fs4_hr_to_mg(z)
			elif full_scale == LIS2DH12.FULL_SCALE_8G:
				x = self.__from_fs8_hr_to_mg(x)
				y = self.__from_fs8_hr_to_mg(y)
				z = self.__from_fs8_hr_to_mg(z)
			elif full_scale == LIS2DH12.FULL_SCALE_16G:
				x = self.__from_fs16_hr_to_mg(x)
				y = self.__from_fs16_hr_to_mg(y)
				z = self.__from_fs16_hr_to_mg(z)

		return x, y, z

	# def status(self):
	# 	'''
	# 	获取芯片状态，包括三轴数据覆盖状态(overrun)和新数据可用状态(data available)
	# 	@return: [zyxor, zor, yor, xor, zyxda, zda, yda, xda]
	# 	'''
	# 	data = self.__read_mem(REG_STATUS_REG)[0]

	# 	return tuple([int(i) for i in f'{data:08b}'])

	# def is_temperature_data_ready(self):
	# 	return self.__get_bit(self.__read_mem(REG_STATUS_REG_AUX)[0], BIT_TDA)

	# def temperature_status(self):
	# 	'''
	# 	获取温度状态，包括数据覆盖状态(overrun)和新数据可用状态(data available)
	# 	@return: (tor, tda)
	# 	'''
	# 	data = self.__read_mem(REG_STATUS_REG_AUX)[0]
	# 	result = [int(i) for i in f'{data:08b}']

	# 	return result[BIT_TOR], result[BIT_TDA]

	def temperature(self):
		'''获取芯片温度数据'''
		result = None
		data = struct.unpack('<h', self.__read_mem(REG_OUT_TEMP_L, 2))[0]

		if data == 0x00:
			result = 0
		elif self.operating_mode == LIS2DH12.MODE_LOW_POWER_8BIT:
			result = self.__from_lsb_lp_to_celsius(data)
		else:
			result = self.__from_lsb_nm_to_celsius(data)

		return result


	#region class properties
	@property
	def data_format(self):
		return LIS2DH12.BIG_ENDIAN \
			if self.__get_bit(self.__read_mem(REG_CTRL_REG4)[0], BIT_BLE) \
			else LIS2DH12.LITTLE_ENDIAN

	@data_format.setter
	def data_format(self, format:int):
		'''设置/获取数据大小端格式'''
		data = self.__read_mem(REG_CTRL_REG4)[0]
		data = self.__set_mode(data, BIT_BLE, format)
		self.__write_mem(REG_CTRL_REG4, data)

	@property
	def temperature_enabled(self):
		return self.__read_mem(REG_TEMP_CFG_REG)[0] == LIS2DH12.TEMP_ENABLED

	@temperature_enabled.setter
	def temperature_enabled(self, enabled:bool):
		'''启用/禁用内置温度传感器'''
		data = LIS2DH12.TEMP_ENABLED if enabled else LIS2DH12.TEMP_DISABLED
		self.__write_mem(REG_TEMP_CFG_REG, data)


	@property
	def block_data_update_enabled(self):
		return True if self.__get_bit(self.__read_mem(REG_CTRL_REG4)[0], BIT_BDU) else False

	@block_data_update_enabled.setter
	def block_data_update_enabled(self, enabled:bool):
		'''
		启用/禁用块数据更新功能
		- 启用：数据块读取后更新
		- 禁用：数据块持续更新
		'''
		data = self.__set_mode(self.__read_mem(REG_CTRL_REG4)[0], BIT_BDU, enabled)
		self.__write_mem(REG_CTRL_REG4, data)

	@property
	def operating_mode(self):
		result = None
		data1 = self.__read_mem(REG_CTRL_REG1)[0]
		data2 = self.__read_mem(REG_CTRL_REG4)[0]

		if self.__get_bit(data1, BIT_LPEN):
			result = LIS2DH12.MODE_LOW_POWER_8BIT
		elif self.__get_bit(data2, BIT_HR):
			result = LIS2DH12.MODE_HIGH_RES_12BIT
		else:
			result = LIS2DH12.MODE_NORMAL_10BIT

		return result

	@operating_mode.setter
	def operating_mode(self, mode:int):
		'''设置/获取操作模式'''
		data1 = self.__read_mem(REG_CTRL_REG1)[0]
		data2 = self.__read_mem(REG_CTRL_REG4)[0]

		if mode == LIS2DH12.MODE_HIGH_RES_12BIT:
			self.__cls_bit(data1, BIT_LPEN)
			self.__set_bit(data2, BIT_HR)
		elif mode == LIS2DH12.MODE_NORMAL_10BIT:
			self.__cls_bit(data1, BIT_LPEN)
			self.__cls_bit(data2, BIT_HR)
		elif mode == LIS2DH12.MODE_LOW_POWER_8BIT:
			self.__set_bit(data1, BIT_LPEN)
			self.__cls_bit(data2, BIT_HR)

		self.__write_mem(REG_CTRL_REG1, data1)
		self.__write_mem(REG_CTRL_REG4, data2)

	@property
	def data_rate(self):
		return self.__get_bits(self.__read_mem(REG_CTRL_REG1)[0], BIT_ORD0, 4)

	@data_rate.setter
	def data_rate(self, power_mode:int):
		'''设置/获取输出数据速率'''
		if power_mode not in LIS2DH12.OUTPUT_DATA_RATES.keys():
			power_mode = LIS2DH12.ORD_POWER_DOWN

		data = self.__set_bits(self.__read_mem(REG_CTRL_REG1)[0], BIT_ORD0, power_mode, 4)
		self.__write_mem(REG_CTRL_REG1, data)

	@property
	def full_scale(self):
		return self.__get_bits(self.__read_mem(REG_CTRL_REG4)[0], BIT_FS0, 2)

	@full_scale.setter
	def full_scale(self, scale:int):
		'''设置/获取满量程数值'''
		data = self.__set_bits(self.__read_mem(REG_CTRL_REG4)[0], BIT_FS0, scale, 2)
		self.__write_mem(REG_CTRL_REG4, data)


	@property
	def spi_mode(self):
		return self.__get_bit(self.__read_mem(REG_CTRL_REG4)[0], BIT_SIM)

	@spi_mode.setter
	def spi_mode(self, mode:int):
		'''设置/获取 SPI 接口模式'''
		data = self.__read_mem(REG_CTRL_REG4)[0]
		data = self.__set_mode(data, BIT_SIM, mode)
		self.__write_mem(REG_CTRL_REG4, data)

	@property
	def sdo_pullup_mode(self):
		return self.__get_bit(self.__read_mem(REG_CTRL_REG0)[0], BIT_SDO_PU_DISC)

	@sdo_pullup_mode.setter
	def sdo_pullup_mode(self, mode:int):
		'''设置/获取 SDO 引脚上拉模式'''
		data = self.__read_mem(REG_CTRL_REG0)[0]
		data = self.__set_mode(data, BIT_SDO_PU_DISC, mode)
		self.__write_mem(REG_CTRL_REG0, data)


	@property
	def click_config(self):
		data = self.__read_mem(REG_CLICK_CFG)[0]
		return tuple([int(i) for i in f'{data:08b}'][2:])



	@click_config.setter
	def click_config(self):
		pass
	#endregion class properties


	#region tools function
	def __from_fs2_hr_to_mg(self, lsb:int):
		return lsb / 16 * 1

	def __from_fs4_hr_to_mg(self, lsb:int):
		return lsb / 16 * 2

	def __from_fs8_hr_to_mg(self, lsb:int):
		return lsb / 16 * 4

	def __from_fs16_hr_to_mg(self, lsb:int):
		return lsb / 16 * 12

	def __from_fs2_nm_to_mg(self, lsb:int):
		return lsb / 64 * 4

	def __from_fs4_nm_to_mg(self, lsb:int):
		return lsb / 64 * 8

	def __from_fs8_nm_to_mg(self, lsb:int):
		return lsb / 64 * 16

	def __from_fs16_nm_to_mg(self, lsb:int):
		return lsb / 64 * 48

	def __from_fs2_lp_to_mg(self, lsb:int):
		return lsb / 256 * 16

	def __from_fs4_lp_to_mg(self, lsb:int):
		return lsb / 256 * 32

	def __from_fs8_lp_to_mg(self, lsb:int):
		return lsb / 256 * 64

	def __from_fs16_lp_to_mg(self, lsb:int):
		return lsb / 256 * 192


	def __from_lsb_lp_to_celsius(self, lsb:int):
		return lsb / 256 * 1 + 25

	def __from_lsb_nm_to_celsius(self, lsb:int):
		return lsb / 64 / 4 + 25

	# def __from_lsb_hr_to_celsius(self, lsb:int):
	# 	return lsb / 64 / 4 + 25


	def __set_bit(self, data:int, index:int):
		'''字节指定位置 1'''
		return data | (1 << index)

	def __cls_bit(self, data:int, index:int):
		'''字节指定位置 0'''
		return data & ~(1 << index)

	def __get_bit(self, data:int, index:int):
		'''获取字节指定位数据'''
		return (data >> index) & 0x1

	def __get_bits(self, data:int, start:int, length:int):
		'''获取字节多位位数据'''
		return (data >> start) & (0xff >> (8 - length))

	def __set_bits(self, data:int, start:int, value:int, length:int):
		'''设置字节多位位数据'''
		v1 = value << start
		v2 = data & (0xff >> (8 - start))
		v3 = data >> (start + length) << (start +length)

		return v3 | v2 | v1

	def __set_mode(self, data:int, index:int, mode):
		'''设置字节指定位置 0 或 置 1'''
		return self.__set_bit(data, index) if mode else self.__cls_bit(data, index)

	def __write_mem(self, mem_addr:int, data):
		'''向指定的寄存器写入数据'''
		if isinstance(data, int):
			data = [data]

		self.__i2c.writeto_mem(self.__i2c_addr, mem_addr, bytearray(data))

	def __read_mem(self, mem_addr:int, nbytes=1):
		'''从指定的寄存器读取数据'''
		if nbytes > 1:
			mem_addr |= 0x80

		return bytearray(self.__i2c.readfrom_mem(self.__i2c_addr, mem_addr, nbytes))
	#endregion tools function


	#region dummy
	_ = 0
	#endregion dummy


if __name__ == '__main__':

	i2c = SoftI2C(scl=Pin(3), sda=Pin(2))
	lis2dh12 = LIS2DH12(i2c, 0x19)

	if lis2dh12.device_id() != LIS2DH12.ID:
		print('invalid lis2dh12 device id')
		exit(0)

	show_log = True

	lis2dh12.reset()

	# lis2dh12.data_format = LIS2DH12.LITTLE_ENDIAN
	print(f'data format: {LIS2DH12.DATA_FORMATS[lis2dh12.data_format]}')

	lis2dh12.block_data_update_enabled = True
	print(f'block data update: {lis2dh12.block_data_update_enabled}')

	lis2dh12.full_scale = LIS2DH12.FULL_SCALE_2G
	print(f'full-scale: {LIS2DH12.FULL_SCALES[lis2dh12.full_scale]}')

	lis2dh12.operating_mode = LIS2DH12.MODE_NORMAL_10BIT
	print(f'operating mode: {LIS2DH12.OPERATING_MODES[lis2dh12.operating_mode]}')

	lis2dh12.data_rate = LIS2DH12.ORD_100HZ
	print(f'data rate: {LIS2DH12.OUTPUT_DATA_RATES[lis2dh12.data_rate]}')

	sleep_ms(90) # 等待传感器稳定


	lis2dh12.temperature_enabled = True
	print(f'temp sensor enabled: {lis2dh12.temperature_enabled}')

	print(f'spi interface mode: {LIS2DH12.SPI_MODES[lis2dh12.spi_mode]}')

	print(f'sdo pull-up mode: {LIS2DH12.SDO_PULL_UP_MODES[lis2dh12.sdo_pullup_mode]}')


	keys = ('BIT_ZD', 'BIT_ZS', 'BIT_YD', 'BIT_YS', 'BIT_XD', 'BIT_XS')
	list(zip(keys, lis2dh12.click_config))

	# status = lis2dh12.status()
	# print(f'status raw: {status}')
	# if show_log:
	# 	print(f'- zyx or: {status[BIT_ZYXOR]}')
	# 	print(f'- zor: {status[BIT_ZOR]}, yor: {status[BIT_YOR]}, xor: {status[BIT_XOR]}')
	# 	print(f'- zyx da: {status[BIT_ZYXDA]}')
	# 	print(f'- zda: {status[BIT_ZDA]}, yda: {status[BIT_YDA]}, xda: {status[BIT_XDA]}')

	# temp_status = lis2dh12.temperature_status()
	# print(f'temp status raw: {temp_status}')
	# if show_log:
	# 	print(f'- t or: {temp_status[0]}')
	# 	print(f'- t da: {temp_status[1]}')


	# while not lis2dh12.is_temperature_data_ready():
	# 	pass
	_ = lis2dh12.temperature()
	lis2dh12.temperature()


	last_x = last_y = last_z = 0
	while False:
		while not lis2dh12.is_zyx_data_ready():
			sleep_ms(1)

		x, y, z = lis2dh12.get_acceleration()

		if last_x != x or last_y != y or last_z != z:
			last_x = x
			last_y = y
			last_z = z

			print(f'x: {x}\t y: {y}\t z:{z}')

		sleep_ms(200)
